import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import net.csdn.www.ImageSave;

import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class HtmlTest {
	private static Set<String> urlList = new HashSet<String>();

	/**
	 * 刘柱栋    2013-3-21
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		//test("http://qing.weibo.com/tag/%E7%BE%8E%E5%A5%B3");
//		URL url = new URL("http://www.xxx881.com/html/article/1.html");
//		Document doc = Jsoup.parse(url,5000);
//		System.out.println(doc);
//		Elements eles = doc.getElementsByClass("n_bd");
//		//for(Element ele : eles){
//		    Element ele = eles.get(0);
//			Elements es = ele.getElementsByTag("img");
//			int i = 0;
//			for(Element e : es){
//				String src_value = e.attr("src");
//				System.out.println(src_value);
//			}
//		//}
		
		vlaiUrlIsTrue();
	}
	
	private static void getImage(Document doc){
		Elements eles = doc.getElementsByClass("n_bd");
		if(eles == null){return;}
	    Element ele = eles.get(0);
		Elements es = ele.getElementsByTag("img");
		for(Element e : es){
			String src_value = e.attr("src");
			System.out.println(src_value);
			ImageSave.saveImg(src_value, "F:\\test10\\xxxx");
		}
	}
	
	private static List<String> getImageUrls(Document doc){
		List<String> links = new ArrayList<String>();
		Elements eles = doc.getElementsByTag("ul");
		if(eles == null){return null;}
	    Element ele = eles.get(0);
		Elements es = ele.getElementsByTag("a");
		for(Element e : es){
			String src_value = e.attr("href");
			links.add(src_value);
		}
		return links;
	}
	
	private static void vlaiUrlIsTrue(){
		/////http://www.xxx881.com/html/part/10_52.html
		String basePath = "http://www.xxx881.com";
		for(int i = 52 ; i > 0; i--){
			String path = "";
			if(i == 1){
				path = basePath + "/html/part/10.html";
			}else{
				path = basePath + "/html/part/10_"+ i +".html";
			}
			Document doc = getDocs(path);
			if(doc != null){
				List<String> links = getImageUrls(doc);
				if(links != null){
					for(String href : links){
						String linkHref =  basePath + href;
						System.out.println(linkHref);
						System.out.println("===============================================");
						Document imgdoc = getDocs(linkHref);
						if(imgdoc != null){
							getImage(imgdoc);
						}
						System.out.println("===============================================");
					}
				}
			}
		}
	}
	
	
	
	private static Document getDocs(String path){
		Document doc = null;
		try{
			URL url = new URL(path);
			doc = Jsoup.parse(url,10000);
			return doc;
		}catch(HttpStatusException e){
			int status = e.getStatusCode();
			if(status == 404){
				return null;
			}
		}catch(MalformedURLException e){
			System.out.println("error:url is not true.....");
			e.printStackTrace();
			System.exit(0);
		} catch (IOException e) {
			System.out.println("error:get url info have error.....");
			e.printStackTrace();
			System.exit(0);
		}
		return null;
	}
	
	private static void test(String path)throws Exception{
		if(path == null || "".equals(path)){
			return;
		}else if(!valiStr(path, "http://")){
			return;
		}
		URL url = null;
		try{
			url = new URL(path);
		}catch(MalformedURLException e){
			System.out.println("the path is not url...........");
			e.printStackTrace();
			return;
		}
		Document doc = Jsoup.parse(url,5000);
		Elements a_url = doc.getElementsByTag("a");
		for(Element link : a_url){
			String link_url = link.attr("href");
			System.out.println("===" + link_url);
			if(urlList.contains(link_url)){
				System.out.println("url已经存在，链表结束................");
			}else{
				urlList.add(link_url);
				test(link_url);
			}
		}
	}
	
	private static boolean valiStr(String str,String parrten){
		int i = str.indexOf(parrten);
		if(i > -1){
			return true;
		}else{
			return false;
		}
	}

}
